<template>
  <div class="xtx-city">
    <!-- 默认显示 -->
    <div class="select" @click="toggleDialog" :class="{ active }">
      <span class="placeholder" v-if="!seled.fullSels">请选择配送地址</span>
      <!-- 展示选择完的结果 -->
      <span class="value" v-else>{{ seled.fullSels }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 选择省、市、区弹层 -->
    <div class="option" v-if="active">
      <span
        class="ellipsis"
        @click="selectCity(city)"
        v-for="city in cityList"
        :key="city.code"
        >{{ city.name }}</span
      >
    </div>
  </div>
</template>
<script>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
// 获取城市列表数据
const useCityList = () => {
  const cityList = ref([])
  // 备份数据变量
  const cityListBackup = []
  onMounted(async () => {
    const { data } = await axios({
      url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
    })
    // 备份数据
    // cityListBackup = data // 改地址了（新开内存）
    cityListBackup.push(...data) // 没有改地址（没开内存）
    // cityList.value = data
    console.log('城市列表：', data)
  })
  return { cityList, cityListBackup }
}
export default {
  name: 'XtxCity',
  // 声明子传父事件名
  emits: ['change'],
  setup (props, { emit }) {
    // 控制选择层是否显示
    const active = ref(false)
    // 获取城市列表数据
    const { cityList, cityListBackup } = useCityList()
    // 点击选择省市区（核心）
    /**
     * 三级联动
     * child => 省（父 默认显示） =》市（子）=》区（子）
     * 选择的数据要不要存？存完提供给外边用（子传父）
     */
    // 点击选中城市数据
    const seled = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countryCode: '', // 地区code
      countryName: '', // 地区名
      // 最终选择(省+市+区)
      fullSels: ''
    })
    // city=> 省 =》市 =》区(没子)
    const selectCity = (city) => {
      console.log('child', city.areaList)
      // 1. 交互
      cityList.value = city.areaList
      // 2. 存储
      if (city.level === 0) {
        // 省
        seled.provinceName = city.name
        seled.provinceCode = city.code
      } else if (city.level === 1) {
        // 市
        seled.cityName = city.name
        seled.cityCode = city.code
      } else {
        // 最后一层 区
        seled.countryName = city.name
        seled.countryCode = city.code
        seled.fullSels = `${seled.provinceName} ${seled.cityName} ${seled.countryName}`
        // 选完了=》1. 关闭选择框 2. 子传父（选中的数据）
        toggleDialog()
        emit('change', seled)
      }
    }
    // 打开弹层
    const open = () => {
      active.value = true
      // 还原
      console.log('open:', cityListBackup)
      cityList.value = cityListBackup
    }
    // 关闭弹层
    const close = () => {
      active.value = false
    }
    const toggleDialog = () => {
      // active.value = !active.value
      if (active.value) {
        close()
      } else {
        open()
      }
    }
    return { active, toggleDialog, cityList, selectCity, seled }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
